package cate.game.framework;

import cate.game.GameBody;
import cate.game.ManyGame;
import cate.game.stat.GameCT;
import cate.game.util.ct.CT;
import cp.game.framework.ApiCaller;
import cp.game.framework.GameTop;
import cp.solution.CpSolutionCommon;
import cp.solution.business.game.status.GameReportPack;
import cp.solution.pattern.Feedback;
import cp.solution.util.device.IpTools;
import lombok.extern.slf4j.Slf4j;

import java.util.ArrayList;

@Slf4j
public class ReportHandler {

    private static long startTime = System.currentTimeMillis();

    public static void sendReport(ApiCaller apiCaller, ManyGame games) {
        try {
            GameReportPack pack = new GameReportPack();
            pack.process.runningId = startTime;
            pack.process.code = games.config.processCode;
            pack.process.groupCode = GameTop.getBootstrap().getProcess().getGroupCode();
            pack.process.excelMode = games.config.excelMode;
            pack.process.hostList = IpTools.getIpV4List();
            pack.process.webScheme = games.config.webScheme;
            pack.process.webPort = games.config.webPort;
            pack.process.hostDomain = GameTop.getBootstrap().getServer().getHostDomain();
            pack.process.wsScheme = games.config.wsScheme;
            pack.process.wsPort = games.config.wsPort;
            pack.process.cloudServerCode = GameTop.getBootstrap().getServer().getCode();
            pack.process.version = GameBody.version() + " " + CpSolutionCommon.version();
            pack.process.detail = games.programInfo.getGitInfo();
            pack.process.srvIdList = new ArrayList<>();
            for(GameBody game : games.getAllIncludeNotOpen()) {
                pack.bodyList.add(makeBodyReport(game));
                pack.process.srvIdList.add(game.config.srvId);
            }
            Feedback feedback = apiCaller.pc().console.gameStatus.report(pack);
            log.info("状态汇报： {}", feedback.text);
        } catch (Exception e) {
            log.error("", e);
        }
    }

    private static GameReportPack.Body makeBodyReport(GameBody game) {
        GameReportPack.Body report = new GameReportPack.Body();
        GameCT ct = new GameCT().init(game);
        report.srvId = game.config.srvId;
        report.srvName = game.config.gs.getName();
        report.srvStatus = game.config.gs.getStatus();
        report.crossCode = game.config.gs.getCrossCode();
        report.onlineNum = ct.onlineNum;
        report.roleNum = game.stat.gameHandler.getRoleNum();
        report.eaten = game.config.gs.hasBeenEaten();
        report.logicRunning = game.isRunning();
        report.openTime = game.config.gs.getOpenTime();
        report.detail = buildDetail(game);
        return report;
    }

    private static String buildDetail(GameBody game) {
        try {
            return game.config.gs.getName() + ":" + CT.gs(game).toDetail() + " " + CT.toDetail();
        } catch (Exception e) {
            return e.getMessage();
        }
    }
}
